import {createRouter, createWebHashHistory} from 'vue-router'
import AppLayout from "@/layout/AppLayout.vue"
import {store} from "@/store";


const router = createRouter({
    history: createWebHashHistory(), //路由模式
    routes: [
        {
            path: '/',
            component: AppLayout,
            children: [
                {
                    path: '',
                    name: 'home',
                    component: () => import('@/views/home/index.vue')
                },
                {
                    path: 'contacts',
                    name: 'contacts',
                    component: () => import('@/views/contacts/index.vue')
                },
                {
                    path: 'center',
                    name: 'center',
                    component: () => import('@/views/center/index.vue')
                },
                {
                    path: 'majority',
                    name: 'majority',
                    component: () => import('@/views/majority/index.vue')
                },
                {
                    path: 'permission',
                    name: 'permission',
                    component: () => import('@/views/permission/index.vue')
                },
                {
                    path: 'aboutUs',
                    name: 'aboutUs',
                    component: () => import('@/views/aboutUs/index.vue')
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/index.vue')
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/views/register/index.vue')
        },
    ], //路由规则
})


router.beforeEach((to, from) => {
    if (store.state.token == '' && to.name != 'login') {
        if (to.name == 'register') {
            console.log(from.name)
        } else {
            return {name: 'login'}
        }
    }
})

export default router
